package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import GUI.equipmentGUI;

import main.Jdbc;

import entity.Model;
import entity.Moisture;
import entity.SenseDevice;

public class MoistureDao {

	private static List<Moisture> moistureInfor;
	
	
	
//	public static void moistureInformation(){
//
//		moistureInfor=new ArrayList<Moisture>();//��
//		
//		try {
////			Date date = new Date();
////			Calendar c = Calendar.getInstance();
////			c.setTime(date);
////			c.add(Calendar.SECOND, -60);
////			date = c.getTime();
////			String sql="SELECT id,device_id,value,create_date " +
////					"FROM cropland_soil_moisture " +
////					"where create_date<'"+date+"' order by create_date desc ";	
//			
//			String sql="SELECT id,device_id,value,create_date " +
//					"FROM cropland_soil_moisture " +
//					" order by create_date desc ";	
//			ResultSet rs=Jdbc.query(sql);
//			
//			while(rs.next()){
//				Moisture  moisture=new Moisture();
//				moisture.setId(rs.getString(1));
//				moisture.setDevice_id(rs.getString(2));
//				moisture.setValue(rs.getString(3));
//				moisture.setCreate_date(rs.getTimestamp(4));
//				
//				moistureInfor.add(moisture);//�
//								
//				}	
//			} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		Jdbc.close();		
//	}
//	
	//����
	public static boolean insert(Moisture moisture){	
	java.util.Date dt = new java.util.Date();
	java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String currentTime = sdf.format(dt);
		//UUID 
		String newId = UUID.randomUUID().toString();
		String sql="insert into cropland_soil_moisture(id,device_id,value,create_date) values(?,?,?,?)";
		int read_data=Jdbc.update(sql,newId, moisture.getDevice_id(),moisture.getValue(),currentTime);
		Jdbc.close();
		moisture.setCreate_date(currentTime);
		equipmentGUI.addTable_Moisture(moisture) ;
		return read_data>0;
		}
		
	public static  List<Moisture> readAll() {
		return moistureInfor;

	}


}
